package com.qf.mvc01.controller;

import com.qf.mvc01.entity.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/user")
public class UserController {

    @RequestMapping("/addUser")
    public String addUser(User user) {
        System.out.println(user);
        return "redirect:listUser";
    }

    @RequestMapping("/delUser/{id}")
    public String delUser(@PathVariable("id") Integer id) {
        System.out.println(id);
        return "redirect:/user/listUser";
    }

    @RequestMapping("/listUser")
    public String listUser() {
        System.out.println("查询数据");
        return "forward:/views/user.html";
    }

    @RequestMapping("/logdd")
    public void login(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html;charset=UTF-8");
        String value = req.getParameter("value");
        String r = null;
        List<String> name = Arrays.asList("路飞", "索隆", "乌索普", "娜美", "基德", "罗", "凯多");
        for (String s : name) {
            if (s.equals(value)) {
                r = "2";
                break;
            } else {
                r = "1";
            }
        }
        resp.getWriter().print(r);

    }

    @RequestMapping("/login")
    public Map<String, String> login(String username, String password, String kapchar, HttpSession session) {

        //获取配置文件验证码数据
        String captcha = (String) session.getAttribute("captcha");
        String s = kapchar.toLowerCase();
        String s1 = captcha.toLowerCase();
        Map<String, String> map = new HashMap<>();
        if (s.equals(s1)) {
            map.put("code", "1");
            map.put("msg", "登入成功");
        } else {
            map.put("code", "-1");
            map.put("msg", "验证码有误");
        }
        return map;
    }
}
